//
//  MoBanOneViewController.m
//  MoBan
//
//  Created by apple on 16/7/6.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MoBanOneViewController.h"
#import "ZBCrossView.h"
#import "ZBTableviewHeadView.h"
#import "ZBCrossOpenModel.h"
@interface MoBanOneViewController ()<ZBCrossViewDatasouce,ZBCrossViewDeleget,UITableViewDataSource,UITableViewDelegate,ZBTableviewHeadViewDeleget>
@property(nonatomic,strong)ZBCrossView *crossview;
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)NSArray *smallarray;
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)ZBCrossOpenModel *openmodel;
@property(nonatomic,assign)BOOL hasClic;
@property(nonatomic,assign)NSInteger inter;

//测试用的
@property(nonatomic,strong)NSMutableArray *arr;
@property(nonatomic,strong)NSMutableArray *arra;
@end

@implementation MoBanOneViewController
-(NSMutableArray *)arra
{
    if (!_arra) {
        _arra = [NSMutableArray array];
    }
    return _arra;
}
-(NSMutableArray *)arr
{
    if (!_arr) {
        _arr = [NSMutableArray arrayWithObjects:@"测试1",@"测试2",@"测试3",@"测试4",@"测试5", nil];
    }
    return _arr;
}
-(UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscrW, KscrH-KBar1)];
        _tableview.dataSource = self;
        _tableview.delegate = self;
//        _tableview.tableHeaderView
        _tableview.tableHeaderView = self.crossview;
    }
    return _tableview;
}
-(NSArray *)smallarray
{
    if (!_smallarray) {
        _smallarray =  @[@"哈",@"真的吗",@"你好啊hehehehheheheh",@"QQ邮箱",@"真是南侧是",@"衣服",@"海伦钢琴的手表"];
    }
    return _smallarray;
}
-(NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}
-(ZBCrossView *)crossview
{
    if (!_crossview) {
        _crossview = [[ZBCrossView alloc]init];
        _crossview.backgroundColor = [UIColor grayColor];
        _crossview.datasouce = self;
        _crossview.deleget = self;
    }
    return _crossview;
}
-(NSArray *)crossView:(ZBCrossView *)crossView
{
    
//    NSLog(@"看看调用每调用");
    return  @[@"哈侧侧侧侧侧侧侧侧侧侧侧侧侧侧侧侧侧侧侧侧",@"真的吗",@"你好啊hehehehheheheh",@"QQ邮箱",@"真是南侧是",@"衣服",@"海伦钢琴的手表"];

}

#warning 用法警告：当该控件用作tableview的tableviewHeadview时要在此刷新tableview并且重新让atbleview的headvview指向该类
-(void)getHightFromView:(ZBCrossView *)crossView WithViewFloat:(CGFloat)hight
{
//    self.crossview.height = hight;
    self.crossview.frame = CGRectMake(0, 0, KscrW, hight);
    [self.tableview reloadData];
    self.tableview.tableHeaderView = crossView;
}
-(void)crossView:(ZBCrossView *)crossView didSelectLable:(NSInteger)index
{
    
    [ZBProssHud showsuccessFrom:self.view with:[NSString stringWithFormat:@"点击了第%ld个",index]];
//    NSLog(@"点击了哪一个%ld",index);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.hasClic = NO;
    for (NSInteger i=0; i<4; i++) {
        ZBCrossOpenModel *model = [[ZBCrossOpenModel alloc]init];
        model.isopen = NO;
        [self.arra addObject:model];
        [self.array addObject:self.smallarray];
    }
    
    
    [self.view addSubview:self.tableview];
    
}


//tableview的使用
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ZBCrossOpenModel *model = self.arra[section];
    
    if (model.isopen==YES) {
        NSArray *array = self.array[section];
        return array.count;
    }
    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
    }
    cell.textLabel.text = self.array[indexPath.section][indexPath.row];
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60*Kscr;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZBTableviewHeadView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"ZBTableviewHeadView"];
    if (!view) {
        view = [[ZBTableviewHeadView alloc]initWithReuseIdentifier:@"ZBTableviewHeadView"];
    }
    view.deleget = self;
    
    ZBCrossOpenModel *model = self.arra[section];
    [view getDatasouce:self.arr[section] WithSection:section isopen:model.isopen];
    return view;

}
#warning 需要再理顺一下
-(void)tableviewHeadView:(ZBTableviewHeadView *)headview didSelectSection:(NSInteger)section
{
    if (self.hasClic) {
        
        if (self.inter!=section) {
            
            ZBCrossOpenModel*model = self.arra[self.inter];
            
                model.isopen = NO;
                
                NSIndexSet *set = [[NSIndexSet alloc]initWithIndex:self.inter];
                
                [self.tableview reloadSections:set withRowAnimation:UITableViewRowAnimationFade];

            
        }
    }
    ZBCrossOpenModel*model = self.arra[section];
    model.isopen =!model.isopen;
    self.hasClic = YES;
    NSIndexSet *set1 = [[NSIndexSet alloc]initWithIndex:section];
    [self.tableview reloadSections:set1 withRowAnimation:UITableViewRowAnimationFade];
    self.inter = section;

}
@end
